package com.xrs.xrseducation.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrs.xrseducation.student.entity.LayuiUtils;
import com.xrs.xrseducation.student.entity.Student;
import com.xrs.xrseducation.student.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-09-18
 */
@RestController
@RequestMapping("/student/student")
public class StudentController {
    @Autowired
    private IStudentService studentServiceImpl;

    @PostMapping("/student")
    @ResponseBody
    public String addStudent(Student student){
        if(!ObjectUtils.isNull(student) && ObjectUtils.isNotEmpty(student) ){
            if(studentServiceImpl.save(student)){
                return "学员添加成功";
            }else{
                return "系统繁忙，请稍后重试";
            }
        }else{
            return "用户信息输入有误，请重试";
        }
    }

    @PutMapping("/student")
    @ResponseBody
    public String updateStudent(Student student){
        if(!ObjectUtils.isNull(student) && ObjectUtils.isNotEmpty(student)){
            if(studentServiceImpl.updateById(student)){
                return "学员修改成功";
            }else{
                return "系统繁忙，请稍后重试";
            }
        }else{
            return "用户信息输入有误，请重试";
        }
    }

    @DeleteMapping("/student/{id}")
    @ResponseBody
    public String deleteStudent(Integer id){
        if(null!=id && !"".equals(id)){
            if(studentServiceImpl.removeById(id)){
                return "学员删除成功";
            }else{
                return "系统繁忙，请稍后重试";
            }
        }else{
            return "非法ID，请重试";
        }
    }

    @GetMapping("/student/{id}")
    @ResponseBody
    public Student getStudentById(Integer id){
        if(null!=id && !"".equals(id)){
            return studentServiceImpl.getById(id);
        }
            return null;
    }

    @RequestMapping("/student")
    @ResponseBody
    public Student queryStudentByUid(Integer uid){
        if(null!=uid && !"".equals(uid)){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("userId",uid);
            return studentServiceImpl.getOne(queryWrapper);
        }else{
            return null;
        }
    }

    @RequestMapping("/queryStu")
    @ResponseBody
    public LayuiUtils queryStudent(Integer page, Integer limit, String did, String valu){
        if(null==page){
            page=1;
        }
        Page page1=new Page(page,limit);
        IPage iPage=studentServiceImpl.queryStu(page1,did,valu);
        if(null!=iPage){
            LayuiUtils utils=new LayuiUtils();
            utils.setData(iPage.getRecords());
            utils.setCode(0);
            utils.setMsg("成功");
            utils.setCount((int)iPage.getTotal());
            return utils;
        }else{
            LayuiUtils utils=new LayuiUtils();
            utils.setData(null);
            utils.setCode(500);
            utils.setMsg("查无此数据");
            utils.setCount(0);
            return utils;
        }
    }


}
